# 购物车Cart
# 购物车属性id
# 方法添加购物车，删除某个商品，清空购物车，购物车商品统计结算
# 商品Prouduct
# 商品属性价格price，id，name，detail

class Product:
    def __init__(self, id,name, price,detail):
        self.name = name
        self.price = price
        self.id = id
        self.detail = detail

class Cart:

    def __init__(self,id):
        self.id = id
        self.cartList = []  # {product:pr,count:1}

    def addCart(self,product,n):
        for cart in self.cartList:
            if cart["product"].name == product.name:
                cart["count"] +=n
                return {"status":200,"msg":"商品数量添加成功"}

        self.cartList.append({"product":product,"count":n})
        return {"status": 200, "msg": "商品添加成功"}

    def deleteCart(self,product):
        for cart in self.cartList:
            if cart["product"].name == product.name:
                self.cartList.remove(cart)

    def clearCart(self):
        self.cartList.clear()


    def payOrderSave(self):
        totalPrice = 0
        for cart in self.cartList:
            totalPrice += cart["product"].price * cart["count"]

        return {"status":200,"totalPrice":totalPrice,"msg":"结算成功"}


if __name__ == '__main__':
    product1 = Product(1,"水杯",15,"喝咖啡很好用")
    product2 = Product(2, "牛奶", 5, "喝牛奶补钙")
    product3 = Product(3, "面包", 3, "早餐吃")

    cart = Cart(1)
    # 查询购物车
    print(cart.cartList)
    # 添加到购物车
    cart.addCart(product1,1)
    cart.addCart(product2,2)
    cart.addCart(product3,2)
    # 查询购物车
    print(cart.cartList)
    cart.addCart(product1,2)
    print(cart.cartList)

    # 删除购物车
    cart.deleteCart(product1)
    print(cart.cartList)

    # 统计价格
    print(cart.payOrderSave())

    cart.clearCart()
    print(cart.cartList)